package com.lm.upms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lm.common.core.util.R;
import com.lm.common.log.annotation.SysLog;
import com.lm.common.log.enums.BusinessType;
import com.lm.common.security.util.SecurityUtils;
import com.lm.upms.api.entity.SysRole;
import com.lm.upms.service.SysRoleMenuService;
import com.lm.upms.service.SysRoleService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 角色
 *
 * @author lm
 * @date 2019/7/2 10:46
 */
@Api(tags = "角色信息")
@Slf4j
@RestController
//@RequestMapping("system/role")
@RequestMapping("role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 分页查询角色信息
     *
     * @param page 分页对象
     * @return
     */
    @GetMapping("/page")
    public R getRolePage(Page page, SysRole sysRole) {
        return R.success(sysRoleService.getListPageBy(page, sysRole));
    }

    /**
     * 通过ID查询角色信息
     *
     * @param id ID
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return R.success(sysRoleService.getById(id));
    }

    /**
     * 添加角色
     *
     * @param sysRole
     * @return
     */
    @SysLog(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping("save")
    public R save(@Valid @RequestBody SysRole sysRole) {
        sysRole.setCreateBy(SecurityUtils.getUser().getUsername());
        return R.success(sysRoleService.insertRole(sysRole));
    }

    /**
     * 修改角色
     *
     * @param sysRole
     * @return
     */
    @SysLog(title = "角色管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "update", produces = "application/json;charset=UTF-8")
    public R update(@Valid @RequestBody SysRole sysRole) {
        return R.success(sysRoleService.updateRole(sysRole));
    }

    /**
     * 删除角色
     *
     * @param ids
     * @return
     */
    @SysLog(title = "角色管理", businessType = BusinessType.DELETE)
    @PostMapping("remove")
    public R remove(String ids) throws Exception {
        return R.success(sysRoleService.removeRoleById(ids));
    }

    /**
     * 获取角色列表
     *
     * @return
     */
    @GetMapping("/list")
    public R listRoles() {
        return R.success(sysRoleService.list(Wrappers.emptyWrapper()));
    }

}
